package com.sq.system.controller;


import com.sq.common.exception.server.ServiceException;
import com.sq.common.web.R;

import com.sq.common.web.base.TableDataInfo;
import com.sq.system.base.BaseController;
import com.sq.system.pojo.SysLoginLog;
import com.sq.system.pojo.SysOperLog;
import com.sq.system.service.SysLoginLogService;
import com.sq.system.vo.ThreadShare;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author Li sir
 * @Date 2024/9/30
 */

@RestController
@RequestMapping("/api/log/")//
public class SysLoginLogController extends BaseController {

    @Autowired
    private SysLoginLogService sysLoginLogService;

    //新增日志
    @PostMapping("saveloginlog")
    public R saveLoginLog(@RequestBody SysLoginLog sysLoginLog){
        int i = sysLoginLogService.saveLoginLog(sysLoginLog);
        return R.ok(i);
    }
    //查询日志
    @PostMapping("loginLogList")
    public TableDataInfo selectLoginLogList(@RequestBody SysLoginLog sysLoginLog){
        startPage(sysLoginLog.getPageNum(),sysLoginLog.getPageSize());
        List<SysLoginLog> list= sysLoginLogService.selectLoginLogList(sysLoginLog);
        return getDataTable(list);
    }


    @PostMapping("eporlog")
    public TableDataInfo selectOperLogList(@RequestBody SysOperLog sysOperLog){

        ThreadShare threadShare = new ThreadShare();
        try {
            Thread getListThread = new Thread(() -> {
                ThreadShare threadSharee = sysLoginLogService.selectSysOperLogReturnShare(sysOperLog);
                threadShare.setRecordList(threadSharee.getRecordList());
                threadShare.setTotal(threadSharee.getTotal());
            });
            getListThread.start();
            getListThread.join(40000L);
            TableDataInfo dataTable = getDataTable(threadShare.getRecordList());
            dataTable.setTotal(threadShare.getTotal());
            return dataTable;
        } catch (InterruptedException e) {
            throw new ServiceException("服务异常,请联系管理员");
        }
    }

}
